<?php

namespace app\ruinuo\controller\design;
use think\facade\Request;
use app\common\model\RuinuoLists;
use app\common\model\RuinuoMode;
use app\common\model\RuinuoDatabase;
use app\common\middleware\Auth;
class Lists
{
    protected $middleware = [Auth::class => []];
    public function Index()
    {
        if (Request::isAjax() || Request::isPost()) {
            $modeid = input('modeid');
            if (empty($modeid)) {
                $data['table'] = [];
                return BackCode($data, '204', '请传入参数');
            }
            $RuinuoLists = new RuinuoLists();
            $arr = $RuinuoLists->where("mode_id='" . $modeid . "'")->field('name,num,id')->orderRaw('sort asc,id desc')->select()->toArray();
            $data['table'] = $arr;
            $RuinuoMode=new RuinuoMode();
            $Mode=$RuinuoMode->where("id='".$modeid."'")->field("name")->findOrEmpty()->toArray();
            $data['title'] = !empty($Mode['name']) ? $Mode['name'] : '';
            return BackCode($data);
        }
        return ViewTpl('ruinuo/design/lists');
    }

    public function TableData()
    {
        $menu_id = input('menu_id');
        if (empty($menu_id)) {
            return BackCode([], '203', '请传入参数');
        }
        $RuinuoLists = new RuinuoLists();
        $find = $RuinuoLists->where("id='" . $menu_id . "'")->field("mode_id,table_cols")->findOrEmpty()->toArray();
        $table_cols=!empty($find['table_cols']) ? json_decode($find['table_cols'], true) : [];
        $data = [
            'cols' => $this->GetCols(),
            'source' => $this->GetSource(),
            'value' => $table_cols,
            'field'=>$this->GetDataField($find['mode_id'],$table_cols)
        ];
        return BackCode($data);
    }

    public function GetDataField($modeid,$value)
    {
        $RuinuoMode=new RuinuoMode();
        $Mode=$RuinuoMode->where("id=".$modeid)->field("database,sub_database")->findOrEmpty()->toArray();
        $table=[$this->GetDataBase($Mode['database'],$value)];
        $sub_database=!empty($Mode['sub_database']) ? json_decode($Mode['sub_database'],true) : [];
        foreach ($sub_database as $k=>$v)
        {

            $table[]=$this->GetDataBase($v['database'],$value,$v['alias']);
        }
        return $table;
    }

    public function GetDataBase($name,$value=[],$modeNum='')
    {
        $RuinuoDatabase=new RuinuoDatabase();
        $table=$RuinuoDatabase->where("name='".$name."'")->field('name,comment,field')->findOrEmpty()->toArray();
        $field = !empty($table['field']) ? json_decode($table['field'], true) : [];
        $children = [];
        foreach ($field as $k=>$v)
        {
            $ValField =$v['field'];
            if ($modeNum)
            {
                $ValField=$modeNum."__".$v['field'];
            }
            $on = false;
            foreach ($value as $kk => $vv) {
                if ($vv['field'] == $ValField) {
                    $on = true;
                }
            }
            $children[] = ['value' => $ValField, 'label' => $v['title'], 'on' => $on];
        }
        $data = [
            'value' => $table['name'],
            'label' => $table['comment'],
            'children' => $children,
        ];
        return $data;
    }

    public function GetSource()
    {
        $source= [
            'position' => [
                'rows' => [
                    ['label' => '左', 'value' => 'left'],
                    ['label' => '中', 'value' => 'center'],
                    ['label' => '右', 'value' => 'right']
                ],
            ],
            'edit' => [
                'rows' => [
                    ['label' => '文本框', 'value' => 'input'],
                    ['label' => '下拉框', 'value' => 'select'],
                    ['label' => '年月日', 'value' => 'date'],
                    ['label' => '年月日时分秒', 'value' => 'time'],
                    ['label' => 'switch', 'value' => 'switch'],
                ],
            ],
            'search' => [
                'rows' => [
                    ['label' => '时间搜索', 'value' => 'datetime'],
                    ['label' => '模糊搜索', 'value' => 'like'],
                    ['label' => '下拉筛选', 'value' => 'select'],
                    ['label' => 'url传参', 'value' => 'url'],
                ],
            ],
            'select' =>GetOptions('options_list')
        ];
        return $source;
    }


    public function GetCols()
    {
        $cols = [
            ['field' => 'field', 'title' => '字段编码', 'minWidth' => 140],
            ['field' => 'title', 'title' => '字段名称', 'minWidth' => 140, 'update' => 'input'],
            ['field' => 'width', 'title' => '宽', 'minWidth' => 90, 'update' => 'input'],
            ['field' => 'group', 'title' => '分组名称', 'minWidth' => 90, 'update' => 'input'],
            ['field' => 'update', 'title' => '编辑方式', 'minWidth' => 120, 'update' => 'select','options'=>'edit'],
            ['field' => 'align', 'title' => '对齐方式', 'minWidth' => 120, 'update' => 'select','options'=>'position'],
            ['field' => 'options', 'title' => '数据源', 'minWidth' => 120, 'update' => 'select','options'=>'select'],
            ['field' => 'search', 'title' => '搜索配置', 'minWidth' => 120, 'update' => 'select','options'=>'search'],
            ['field' => 'hide', 'title' => '隐藏', 'minWidth' => 80, 'update' => 'switch'],
            ['field' => 'count', 'title' => '合计', 'minWidth' => 80, 'update' => 'switch'],
            ['field' => 'sort', 'title' => '排序', 'minWidth' => 80, 'update' => 'switch'],
            ['field' => 'tpl', 'title' => '模板', 'minWidth' => 90, 'fixed' => 'right','tpl'=>'<div><button type="button" class="pear-btn pear-btn-primary pear-btn-smm" onclick="layui.design_table.BindTpl(this)">配置</button></div>'],
        ];
        return $cols;
    }

    public function ColsAdd()
    {
        $checked = input('checked');
        $id = input('id');
        $title = input('title');
        $num = input('num');
        $RuinuoLists = new RuinuoLists();
        $find = $RuinuoLists->where("id='" . $id . "'")->field("mode_id,table_cols")->find();
        if (empty($find)) {
            return BackCode([], 203, '沒有此菜单');
        }
        $item = !empty($find['table_cols']) ? json_decode($find['table_cols'], true) : [];
        if ($checked == 1) {
            $ids = time() . '_' . $id . '_' . count($item);
            $item[] = ['field' => $num, 'title' => $title, 'id' => $ids];
        } else {
            foreach ($item as $k => $v) {
                if ($v['field'] == $num) {
                    unset($item[$k]);
                }
            }
        }
        $item=array_values($item);
        $updata['table_cols'] = !empty($item) ? json_encode($item, JSON_UNESCAPED_UNICODE) : '[]';
        $RuinuoLists->where("id='" . $id . "'")->update($updata);
        return BackCode($item);
    }

    public function editcols()
    {
        $item = !empty(input('item')) ? json_decode(input('item'), true) : [];
        foreach ($item as $k => $v) {
            unset($item[$k]['LAY_NUM']);
            unset($item[$k]['LAY_INDEX']);
        }
        $item=array_values($item);
        $id = input('id');
        $type = input('type');
        $updata['table_cols'] = !empty($item) ? json_encode($item, JSON_UNESCAPED_UNICODE) : '[]';
        $RuinuoLists = new RuinuoLists();
        $RuinuoLists->where("id='" . $id . "'")->update($updata);
        return BackCode();
    }

    public function MenuCopy()
    {
        if (Request::isAjax() || Request::isPost()) {
            $id = input('id');
            if (empty($id))
            {
                return BackCode([],203,'参数ID不能为空');
            }
            $RuinuoLists = new RuinuoLists();
            $find = $RuinuoLists->where("id='" . $id . "'")->withoutField("id")->findOrEmpty()->toArray();
            if (empty($find))
            {
                return BackCode([],203,'没有此数据');
            }
            $find['num']='m_'.time();
            $find['sort']=!empty($find['sort']) ? $find['sort']+1 : 1;
            $find['create_id']=GetToken('id');
            $find['update_id']=$find['create_id'];
            $find['create_time']=date('Y-m-d H:i:s');
            $find['update_time']=$find['create_time'];
            $upId=$RuinuoLists->insertGetId($find);
            if ($upId)
            {
                return BackCode();
            }
            return BackCode([],203,'复制失败');
        }
    }
}
